//
//  ViewController.swift
//  HBImageTableView
//
//  Created by 伍宏彬 on 2017/5/9.
//  Copyright © 2017年 whb. All rights reserved.
//

import UIKit

extension Selector {
    static let BtnChick = #selector(ViewController.chooseChick(btn:))

}

extension UIButton {

    convenience init(frame:CGRect, title: String) {
        self.init()
        
        self.frame = frame
        self.setTitle(title, for: .normal)
        
    }

}

class ViewController: HBImageViewController {
    
    fileprivate lazy var headView: UIView = {
        
        let hView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 200))
        hView.backgroundColor = UIColor.brown
        hView.layer.masksToBounds = true
        
        return hView
    }()
    
    fileprivate lazy var imageView: UIImageView = {
        
        let iView = UIImageView(image: UIImage.init(named: "timg.jpeg"))
        iView.contentMode = .scaleAspectFill
        iView.frame = self.headView.bounds
        
        return iView;
        
    }()
    
    fileprivate lazy var headBarView: UIView = {
        
        let hView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 44))
        hView.backgroundColor = UIColor.red
    
        return hView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        let one = ViewController_one()
        one.title = "第一个"
        
        let two = ViewController_one()
        two.title = "第二个"
        
        let three = ViewController_one()
        three.title = "第三个"
        
        let four = ViewController_one()
        four.title = "第四个"
        
        headView.addSubview(imageView)
        
        self.image_add(ChildVCs: [one, two, three, four], headView: headView, barView: headBarView)
        
        
        let btnW = headBarView.hb_W / CGFloat(self.childViewControllers.count)
        
        
        for (index, baseVc) in self.childViewControllers.enumerated() {
            print(baseVc.title ?? "nothing")
            
            let btn = UIButton(frame: CGRect(x: CGFloat(index) * btnW, y: 0, width: btnW, height: headBarView.hb_H), title: baseVc.title!)
            btn.tag = index
            
            btn.setTitleColor(UIColor.white, for: .normal)
            btn.setTitleColor(UIColor.blue, for: .selected)
            
            btn.addTarget(self, action: Selector.BtnChick, for: .touchUpInside)
            
            headBarView.addSubview(btn)
            
        }
        
    }
    
    @objc func chooseChick(btn: UIButton) {
        
        print(btn.tag)
//        btn.isSelected = true
    }
    
    override func image_scrollView(scrollView: UIScrollView, willScrollToPageIndex nextPage: Int, currentPageIndex currentPage: Int) {
        
        let lastBtn = headBarView.subviews[currentPage] as! UIButton
        lastBtn.isSelected = false
        
        
        
        let selectBtn = headBarView.subviews[nextPage] as! UIButton
        selectBtn.isSelected = true
        
        print("当前页码：\(currentPage) 滚动到：\(nextPage)")
    }
   
    override func image_scrollView(scrollView: UIScrollView, didScrolle contentOffset: CGPoint) {
//        print("didScrolle: \(contentOffset.x)")
    }
   
}

class ViewController_one: UIViewController, ChildVcProtocol {
    
    func getChildVc() -> UIViewController {
        return self
    }
    
    func getTableView() -> UITableView {
        return self.tableView
    }
    
    lazy var tableView: UITableView = {
        
        let hTableView = UITableView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height), style: .plain)
        hTableView.delegate = self
        hTableView.dataSource = self
        return hTableView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cellid")
        
        view.addSubview(tableView)
        // Do any additional setup after loading the view.
        
    }
    
    

}
extension ViewController_one: UITableViewDataSource, UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 300
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cellid", for: indexPath)
        cell.textLabel?.text = (self.title ?? "默认标题") + "-" + "\(indexPath.row)"
        return cell
        
    }

    
}
